import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException
import com.google.gson.TypeAdapter
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter

/**
 * Created by bshao on 10/16/15.
 */

def testSer() {
    Gson gson = new Gson();

    def user = new User(name: "haha", phone: "hehe")

    def json = gson.toJson(user)

    println json
}

def testDeser() {
    def text = """\
{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}
"""
    Gson gson = new Gson()
    def profile = gson.fromJson(text, Profile.class)
    println(profile)
}

def testMap() {
    def map = [a: true, b: false]
    Gson gson = new Gson()

    def json = gson.toJson(map);

    println(json)

    def booleanMap = gson.fromJson(json, new TypeToken<Map<String, Boolean>>() {}.getType())

    println(booleanMap)
}

class Result {
    BigDecimal a = BigDecimal.ZERO;
    BigDecimal b = BigDecimal.ZERO;
    String c;


    @Override
    public String toString() {
        return """\
Result{
    a=$a,
    b=$b,
    c='$c'
}"""
    }
}



def testBigDecimal() {
    def text = """\
{
  "a": null,
  "b": 1.1,
  "c": null
}
"""

    /**
     * 自定义 String TypeAdapter ， null 转换为
     */
    TypeAdapter<String> STRING = new TypeAdapter<String>() {
        public String read(JsonReader reader) {
            try {
                if (reader.peek() == JsonToken.NULL) {
                    reader.nextNull();
                    return "";
                }
                return reader.nextString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "";
        }

        public void write(JsonWriter writer, String value) {
            try {
                if (value == null) {
                    writer.nullValue();
                    return;
                }
                writer.value(value);
            } catch (Exception e) {
                throw new JsonSyntaxException(var3);
            }
        }
    };

    /**
     * 自定义TypeAdapter ,null对象将被解析成空字符串
     */
    TypeAdapter<BigDecimal> BIG_DECIMAL = new TypeAdapter<BigDecimal>() {
        public BigDecimal read(JsonReader reader) throws IOException {
            if(reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return BigDecimal.ZERO;
            } else {
                try {
                    return new BigDecimal(reader.nextString());
                } catch (NumberFormatException var3) {
                    throw new JsonSyntaxException(var3);
                }
            }
        }

        public void write(JsonWriter out, BigDecimal value) throws IOException {
            out.value(value);
        }
    };

    GsonBuilder gsonBulder = new GsonBuilder();
    gsonBulder.registerTypeAdapter(String.class, STRING);
    gsonBulder.registerTypeAdapter(BigDecimal.class, BIG_DECIMAL);

    def gson = gsonBulder.create()

    def result = gson.fromJson(text, Result.class)

    println result
}

//testSer()
//testDeser()
//testMap()

testBigDecimal()